﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace Shrestha.Trigraph
{
    /// <summary>
    /// Interaction logic for DataSourceLoader.xaml
    /// </summary>
    public partial class DataSourceLoader : Window
    {
        //double percentComplete, totalData;
        private DbManager mDbManager;
        private WorkingSet workingSet;

        public WorkingSet CurrentSet
        {
            get { return workingSet; }
            set { workingSet = value; }
        }

        public DataSourceLoader()
        {
            InitializeComponent();
            mDbManager = new MySqlDbManager();
        }

        private void buttonLoad_Click(object sender, RoutedEventArgs e)
        {
            InActivateTextfields();
            ShowProgressBar();
            if (mDbManager.InitDb(textBoxHost.Text, textBoxDbName.Text, textBoxUsername.Text , textBoxPassword.Password))
            {
                Thread loader = new Thread(startLoading);
                try
                {
                    loader.Start();
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }

                loader.Join();

                MessageBox.Show("Completed.","Database import");
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("Could not connect to the database.", "Database error", MessageBoxButton.OK, MessageBoxImage.Error);
                this.DialogResult = false;
            }
        }

        private void InActivateTextfields()
        {
            this.textBoxDbName.IsReadOnly = this.textBoxHost.IsReadOnly = this.textBoxPassword.IsEnabled = this.textBoxUsername.IsReadOnly = true;
            this.buttonLoad.IsEnabled = false;
        }

        private void ShowProgressBar()
        {
            this.progressBarData.IsEnabled = true;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void startLoading()
        {
            string tableName = " pedestrian ";
            string latName = " Lat ";
            string lngName = " Lng ";
            string timeName = " EventDate ";

            //string tableName = " war_diary ";
            //string latName = " Latitude ";
            //string lngName = " Longitude ";
            //string timeName = " EventDate ";

            this.workingSet = mDbManager.ExtractParameters(tableName, latName, lngName, timeName);
        }
    }
}
